package com.answeringplayer.service.Impl;

import com.answeringplayer.Bean.Response;
import com.answeringplayer.dto.PracticeRecordDTO;
import com.answeringplayer.dto.WrongQuestionDTO;
import com.answeringplayer.exception.BusinessException;
import com.answeringplayer.mapper.PracticeRecordMapper;
import com.answeringplayer.mapper.WrongQuestionMapper;
import com.answeringplayer.service.QueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class QueryServiceImpl implements QueryService {

    @Autowired
    private PracticeRecordMapper practiceRecordMapper;

    @Autowired
    private WrongQuestionMapper wrongQuestionMapper;

    @Override
    public Response queryPracticeRecord(Long userId) {
        if (userId == null || userId <= 0) {
            throw new BusinessException(400, "用户ID无效");
        }

        List<PracticeRecordDTO> practiceRecordDTOS;

        try {
            practiceRecordDTOS = practiceRecordMapper.selectByUserId(userId);
        } catch (Exception e) {
            throw new BusinessException(500, "查询练习记录异常");
        }

        if (practiceRecordDTOS == null || practiceRecordDTOS.isEmpty()) {
            return Response.success("无历史记录");
        }

        return Response.success(practiceRecordDTOS);
    }

    @Override
    public Response queryWrongQuestion(Long userId) {
        if (userId == null || userId <= 0) {
            throw new BusinessException(400, "用户ID无效");
        }

        List<WrongQuestionDTO> wrongQuestionDTOS;

        try {
            wrongQuestionDTOS = wrongQuestionMapper.selectByUserId(userId);
        } catch (Exception e) {
            throw new BusinessException(500, "查询错题异常");
        }

        if (wrongQuestionDTOS == null || wrongQuestionDTOS.isEmpty()) {
            return Response.success("无错题记录");
        }

        return Response.success(wrongQuestionDTOS);
    }
}